import request from "@/utils/request";
import store from "../store/modules/user"


export async function login(params) {
    return await request({
        url: "/user",
        params: params
    })
}

export async function addUser(params) {
    return await request({
        url: "/user",
        method: "post",
        data: params
    })
}

export async function addUserList(params) {
    let isOk = true
    let data = []
    for (var item of params) {
        try {
            let res = await addUser(item)
            data.push(res.data.id)
        } catch (err) {
            console.log(err)
            isOk = false
            return err
        }
    }
    if (isOk) {
        return {
            code: 200,
            msg: "添加成功",
            data
        }
    } else {
        return {
            code: 201,
            msg: "添加失败",
            data
        }
    }
}

export async function checkUser(params) {
    return await request({
        url: "/user",
        params: params
    })
}

export async function editor(id, params = {}) {
    return await request({
        url: "/user/" + id,
        method: "patch",
        data: params
    })
}

export async function getUserInfo(id) {
    return await request({
        url: "/user/" + id
    })
}

export async function getUserAll() {
    return await request({
        url: "/user"
    })
}

export async function getUserList(_page = 1, _limit = 5) {
    let { data } = await getUserAll()
    let total = data.filter(item => item.type != 0).length

    let res = await request({
        url: "/user",
        params: {
            _page,
            _limit,
            type: 1
        }
    })
    return {
        ...res,
        total
    }
}

export async function delUser(id) {
    return await request({
        url: "/user/" + id,
        method: "delete"
    })
}

export async function resetPass(id) {
    return await request({
        url: "/user/" + id,
        method: "patch",
        data: {
            password: "admin123"
        }
    })
}